package code.hkgolden.android.core;

import java.io.Serializable;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicInteger;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ProgressBar;

public class HkgoldenTaskManager {

	public static final int STOP = 0x10000;
	public static final int REQUEST = 0x10001;
	public static final int REQUEST_PROGRESS = 10 * 100;
	public static final int LOAD = 0x10002;
	public static final int LOAD_PROGRESS = 20 * 100;
	public static final int RENDER = 0x10003;
	public static final int RENDER_PROGRESS = 70 * 100;

	public static class ProgressHandler extends Handler {
		int processes;
		AtomicInteger progress;
		// ExecutorFacade executorFacade;
		// Activity activity;
		ProgressBar progressBar;

		public ProgressHandler(ProgressBar progressBar) {
			this.progress = new AtomicInteger(0);
			this.progressBar = progressBar;
		}

		public void sendSerializableCollection(Collection<?> collection,
				Handler handler) {
			if (collection != null && collection.size() > 0) {
				this.processes = collection.size();
				// this.executorFacade = new ExecutorFacade(this.processes);
				for (Serializable serializable : (Collection<Serializable>) collection) {
					sendSerializable(serializable, handler);
				}
			}
		}

		public void sendSerializable(Serializable serializable, Handler handler) {
			if (Serializable.class.isAssignableFrom(serializable.getClass())) {
				final Bundle b = new Bundle();
				b.putSerializable(serializable.getClass().getSimpleName(),
						serializable);
				final Message msg = Message.obtain(handler);
				msg.setData(b);
				msg.sendToTarget();
			}
		}

		public void notify(int status) {
			final Message msg = Message.obtain(this);
			msg.what = status;
			msg.sendToTarget();
		}

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case REQUEST:
				progressBar.setVisibility(View.VISIBLE);
				progress.set(REQUEST_PROGRESS);
				progressBar.setProgress(progress.get());
				break;
			case LOAD:
				progressBar.setProgress(progress.addAndGet(LOAD_PROGRESS));
				break;
			case RENDER:
				progressBar.setProgress(progress
						.addAndGet((RENDER / processes + 1) * 100));
				if (progress.get() < 100 * 100) {
					break;
				}
			case STOP:
				progressBar.setVisibility(View.GONE);
				break;
			}
		}
	}

	public void sendSerializableCollection(Collection<?> collection,
			Handler handler) {
		if (collection != null && collection.size() > 0) {
			for (Serializable serializable : (Collection<Serializable>) collection) {
				sendSerializable(serializable, handler);
			}
		}
	}

	public void sendSerializable(Serializable serializable, Handler handler) {
		if (Serializable.class.isAssignableFrom(serializable.getClass())) {
			final Bundle b = new Bundle();
			b.putSerializable(serializable.getClass().getSimpleName(),
					serializable);
			final Message msg = Message.obtain(handler);
			msg.setData(b);
			msg.sendToTarget();
		}
	}

}
